package algocasts;

public class P83_reverseWords {
    public String reverseWords(String s) {
        if (s == null || s.length() == 0) return s;
        char[] c = s.toCharArray();
        int start = 0, end = 0;
        while (start < c.length) {
            while (end < c.length && c[end] != ' ') ++end;
            for (int i = start, j = end - 1; i < j; ++i, --j) {
                char tmp = c[i];
                c[i] = c[j];
                c[j] = tmp;
            }
            start = end + 1;
            end = start;
        }
        return new String(c);
    }

    public static void main(String[] args) {
        P83_reverseWords solution = new P83_reverseWords();
        System.out.println(solution.reverseWords("I am busy"));
    }
}
